clear all
set more off
	
global dir = "C:\Users\mrueda\Documents\Emory\Papers\Networks_persistance\do_files\do_files_APSA21\post_JOP\Replication_BJPS\"	

cd "$dir"

use "Data\donor_level_persist_rep.dta",clear

gen treat=0
replace treat=1 if margin_victory>0&margin_victory~=.
replace treat=. if margin_victory==.

keep if rank==1|rank==2
replace b5=. if b2!=0

gen treat_margin_victory=treat*margin_victory




	texdoc close 
	cap erase "$dir/Tables/TableG5.tex"
	texdoc init "$dir/Tables/TableG5.tex", force

	tex \begin{table}[tbph]
	tex \caption{Effect of donating to an election winner on future donations for close elections (donor-level)}\label{tab:donations_d_ols_cl}
	tex \centering
	tex \begin{tabular}{l c c H} \hline
	tex Outcome:&  Any race  & Mayor & Other races \\
	tex & (1) & (2) & (3) \\ \hline
	tex & & & \\
	
	*Model 1
	foreach var in donate_15any b5{
	
		quietly: rdrobust `var' margin_victory, p(1) vce(cluster muni_code) 
		
		*Local's for the table
		local bw : di %5.2f `e(h_l)'
		
		*No Family
		*Regressions
		quietly: regress `var' treat sanc_before ilegal p_prop elec_exp pol_exp_d center rank_don_alt_all lcont_donor_102 contraloria above_lim if margin_victory<=`bw',vce(cluster muni_code)
		quietly sum `var' if e(sample)
			local mean_`var' : di %5.3f r(mean)
			local sd_`var' : di %5.3f r(sd) 		
		
		regress `var' treat sanc_before ilegal p_prop elec_exp pol_exp_d center rank_don_alt_all lcont_donor_102 contraloria above_lim if margin_victory<=`bw',vce(cluster muni_code)

		local N_`var' : di %5.0f e(N)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local b1_`var' : di %5.3f b[1,1]
		local se1_`var' : di %5.3f sqrt(v[1,1])
		local p_v_`var' :di %5.3f res[4,1]
		local uci_`var': di %5.3f res[6,1]
		local lci_`var': di %5.3f res[5,1]
		
		areg `var' treat sanc_before ilegal p_prop elec_exp pol_exp_d center rank_don_alt_all lcont_donor_102 contraloria above_lim if e(sample), absorb(muni_code) vce(cluster muni_code)

		local Nf_`var' : di %5.0f e(N)

		matrix bf = e(b)
		matrix vf = e(V)
		matrix resf=r(table)
		
		local b1f_`var' : di %5.3f bf[1,1]
		local se1f_`var' : di %5.3f sqrt(vf[1,1])
		local p_vf_`var' :di %5.3f resf[4,1]
		local ucif_`var': di %5.3f resf[6,1]
		local lcif_`var': di %5.3f resf[5,1]
		
	}
	

	*Continue table
	tex Electoral victory & `b1_donate_15any' & `b1_b5' & `b1_b2b' \\
	tex \ \ \ \ p-value & `p_v_donate_15any' & `p_v_b5' & `p_v_b2b' \\
	tex \ \ \ \ CI 95\%  & [`lci_donate_15any',`uci_donate_15any'] & [`lci_b5',`uci_b5'] & [`lci_b2b',`uci_b2b'] \\
	tex & & & \\
	tex Electoral victory (FE) & `b1f_donate_15any' & `b1f_b5' & `b1f_b2b' \\
	tex \ \ \ \ p-value & `p_vf_donate_15any' & `p_vf_b5' & `p_vf_b2b' \\
	tex \ \ \ \ CI 95\%  & [`lcif_donate_15any',`ucif_donate_15any'] & [`lcif_b5',`ucif_b5'] & [`lcif_b2b',`ucif_b2b'] \\
	tex & & & \\
	
	
	tex Observations & `N_donate_15any' & `N_b5' & `N_b2b' \\
	tex Mean & `mean_donate_15any' & `mean_b5' & `mean_b2b' \\ \hline
	tex \end{tabular}
	tex \parbox{160mm}{ \footnotesize{
	tex \footnotesize{\footnotesize{Ordinary least squares (OLS) estimates of the effect of donating to the winner on donating in the next election. Samples correspond to observations within RD MSE optimal bandwidth. All models include as controls: candidate's illegal registration of ID, being sanctioned by the Office of the Inspector General, elected posts, ran as candidate in past elections, party is not left-wing or right-wing, non-family donations as a fraction of campaign revenue, logged value of donation, donated above legal limit, sanctioned, and rank of donation among all donors. FE denote municipality fixed effects. Confidence intervals and p-values with clusters at the municipality level.
	tex }
	tex }
	tex }}
	tex \end{table}
	cap texdoc close 
